﻿
Imports std
Imports centrosphere
Imports centrosphere.job
Imports centrosphere.job.storage

Partial Public Class storageJobRedundanceDistributor
    Private Function verify_distribute(ByVal inputJob As ijob,
                                       ByVal targetJobs As vector(Of pair(Of UInt32, ijob))) As Boolean
        If passVerifyRate() Then
            Return hash_redundance_distribute(inputJob, targetJobs)
        Else
            Return redundance_hash_random_distribute(inputJob, targetJobs)
        End If
    End Function

    Private Shared Function required_init(ByVal i As ijob, ByRef j As storageJob) As Boolean
        j = storageJob(i)
        Return centrosphere.storageJob.required_init(j) AndAlso eva(j.result, False)
    End Function

    Protected Overrides Function distribute(ByVal inputJob As ijob,
                                            ByVal targetJobs As vector(Of pair(Of UInt32, ijob)),
                                            ByRef waitall As Boolean) As Boolean
        Dim j As storageJob = Nothing
        If valid(inputJob) AndAlso required_init(inputJob, j) Then
            waitall = True
            If distribute_verifiable_job(j) Then
                Return verify_distribute(inputJob, targetJobs)
            ElseIf storage.hashable(j.command) Then
                Return hash_redundance_distribute(inputJob, targetJobs)
            Else
                Return spread_distribute(inputJob, targetJobs)
            End If
        Else
            Return False
        End If
    End Function
End Class
